function [RX_IN_Filtered_out,First_Sample, Rate_Index,Interleaver_Index] = ...
    HF_TimeSynch(RX_IN_Filtered,Oversample, Original_RX_IN, EsNoRatio, Original_Start,...
    N1, N2)

%*------------------------------------ HF_TimeSynch -----------------------------------%
%
% HF_TimeSynch
%this function senses the channel and decide where is the first symbol
%location
%Function inputs are sensed signal and oversample value
%Function outputs are signal after filtering with SRRC, the first sample of
%first symbol position, data rate value, and interleaver size
%
%*------------------------------------------------------------------------------------%

% prepare the needed data in the code
%------------------------------------
global Sync_Preamble Mini_Probe_Plus LUT_D

Mini_Probe_Plus_Modulated =  HF_Modulate(Mini_Probe_Plus,4800);
Initial_Sync_Preamble = HF_Modulate(Sync_Preamble,4800);

Mini_Probe_Plus_Modulated_Upsampled = upsample(Mini_Probe_Plus_Modulated,Oversample);
Initial_Sync_Preamble_Upsampled = upsample(Initial_Sync_Preamble,Oversample);

% Energy Detection 
% ---------------------
start = 0;
thershold = 15;
sum_s = 0;
while(sum_s < thershold)
    sum_s =  sum(abs(RX_IN_Filtered(start+1:100+start+1)));
    start = start+1;
end

% detect the location of first symbol in data signal
%---------------------------------------------------
sync_flag = start;
First184 = 0;
while (First184 == 0 )
    % take the first 184 symbols in incoming sensed signal when sync_flag
    % is zero value and shift the taken signal by sync_flag incrementing
    First184_RxInFiltered = RX_IN_Filtered(sync_flag:sync_flag+2*184*Oversample-1);
    % calculate first 184 symbols energy
    First184_Energy = sum(abs(First184_RxInFiltered(1:1*184*Oversample)));
    % correlate between first 184 symbols in sensed signal and initial sync
    % preamble symbols (184 symbols)
    yy=conv( conj(fliplr(Initial_Sync_Preamble_Upsampled)) , (First184_RxInFiltered) );
    sync_flag =sync_flag+1*184*Oversample;
    
    %check from the max value of correlated signal if the sensed signal is
    %real signal or noise by comparing it with threshold
    
    Th_real = First184_Energy/14;
    
    %     max(abs(yy))
    if length(find(abs(yy) > Th_real)) > 1
        % if there are two or more places in correlated signal then it is
        % real data signal and not noise
        First184 = 1;
    end
end

% get the peak position in correlated signal (the last sample in the
% initial preamble // MH
[Preamble_places N_peaks] = GetPeak_Indecies(abs(yy),Th_real, 2);

if(N_peaks == 1)
    [Preamble_Place1 N_peaks] = GetPeak_Indecies(abs(yy(Preamble_places-20-5:Preamble_places-20+5)),Th_real/4, 1);
    Preamble_Place1 = Preamble_Place1 + Preamble_places - 20 - 5 - 1;
    Preamble_Place2 = Preamble_places;
    
    if(isempty(Preamble_Place1))
        InitialPreamble_Place = Preamble_Place2+start-1;
    else
        InitialPreamble_Place = Preamble_Place1+start-1;
    end
else
    InitialPreamble_Place = Preamble_places(1)+start-1;
end


% idealy the phase of peak shall be zero, but due to channel error we may
% to correct it if there is phase error
Phase_Shift = angle(yy(InitialPreamble_Place));


% detect the location of first symbol in mini-probe preamble
%-----------------------------------------------------------
First31 = 0;
First31Symbol_Preample = Initial_Sync_Preamble_Upsampled(1:31*Oversample);

while (First31 == 0 )
    % take the first 31 symbols after which discovered -take the first 31 from
    % undiscovered signal- from sensed signal
    First31_RxInFiltered = RX_IN_Filtered(InitialPreamble_Place:InitialPreamble_Place+31*Oversample-1);
    % correlate it with the first 31 in initial preamble
    Withfirst31preamble = conv(First31_RxInFiltered , conj(fliplr(First31Symbol_Preample)));
    % correlate it with the mini-probe symbols
    Withminiprobe = conv(First31_RxInFiltered , conj(fliplr(Mini_Probe_Plus_Modulated_Upsampled)));
    
    if max(abs(Withminiprobe)) > max(abs(Withfirst31preamble))
        % the undiscovered 31 symbols is for miniprobe
        First31 = 1;
    else
        % pass the 184 initial preamble symbols
        InitialPreamble_Place = InitialPreamble_Place + Oversample*184;
    end
end

% First Sample of last synchronization preamble (the first of the last 184 symbols)
First_Sample = InitialPreamble_Place - Oversample*184;


%*------------------------------------ CORRECTING THE INITIAL PHASE -----------------------------%
% the initial phase results from the difference between carrier phases in
% TX and RX
RX_IN_Filtered_out = RX_IN_Filtered ; %.* exp(-1*1i*Phase_Shift);
%*-----------------------------------------------------------------------------------------------%





% ====================================================================
% Estimation And Equalization of the 41 symbols that hold the information using Linear equalizer// by MH
% ====================================================================

Training_Length = 31;
Data_Length = 41;
estimation_length = 31*Oversample;
ChLength = 6*Oversample;
DataAfter_Length = 31;

N = N1 + N2 + 1;

Start = First_Sample + 184 * Oversample;
Original_Start_used = (Original_Start + 1) + (184 + 31) * Oversample;

OUT_Symbols = MMSE_DFE_Equalizer(RX_IN_Filtered_out, Original_RX_IN, Oversample, EsNoRatio, 4800,...
    1, N1, N2, Training_Length, Data_Length, estimation_length, ChLength, DataAfter_Length, Start, Original_Start_used);

%----------------------------- Rate & Interleaver Size Information Extraction -------------------------------------%
Info_Symbols = HF_Demodulate(OUT_Symbols, 4800);

SubtractionSeq = [0 4 0 4 0 0 4 4 0 0 0 0 0];

% get the most value repeated in Info_Symbols(x:x+12) - SubtractionSeq
D0_values = Info_Symbols(2:14) - SubtractionSeq;
D1_values = Info_Symbols(15:27) - SubtractionSeq;
D2_values = Info_Symbols(28:40) - SubtractionSeq;

D0 = mode(D0_values);           % mode is used to get the most frequently value in D0_values // MH
D1 = mode(D1_values);
D2 = mode(D2_values);

LUT_D0 = reshape(LUT_D(:,:,1),1,[]) ;
LUT_D1 = reshape(LUT_D(:,:,2),1,[]) ;
LUT_D2 = reshape(LUT_D(:,:,3),1,[]) ;

D0_loc = find(LUT_D0 == D0);
D1_loc = D0_loc(find(LUT_D1(D0_loc) == D1));
D2_loc = D1_loc(find(LUT_D2(D1_loc) == D2));

Data_Rates = [3200 4800 6400 8000 9600 3200 4800 6400 8000 9600 3200 4800 6400 8000 9600 3200 4800 6400 8000 9600 3200 4800 6400 8000 9600 3200 4800 6400 8000 9600];
Interleaver_Lengths = [1 1 1 1 1 3 3 3 3 3 9 9 9 9 9 18 18 18 18 18 36 36 36 36 36 72 72 72 72 72];

if(D0 == 6 && D1 == 6 && D2 == 2)
    Rate_Index = 12800;
    Interleaver_Index = 1;
else
    Rate_Index = Data_Rates(D2_loc);
    Interleaver_Index = Interleaver_Lengths(D2_loc);
end




end









